package com.probiz.estore.system.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.system.AppAdmin;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.system.dao.AppAdminDao;
import com.probiz.estore.system.model.AppAdminSearchCriteria;


/**
 * AppAdmin Data Access Object (DAO) implementation.
 * Developer introduced interfaces should be declared here. Won't get overwritten.
*/
public class AppAdminDaoImpl extends AppUserDaoImpl<AppAdmin> implements AppAdminDao {

	@Override
	public void initFilterHqlMap(){
		this.filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT,"select s from AppAdmin s where s.deleted!=1 order by s.appuserId desc");
	}
	
	public void removeAppAdmin(Integer appAdminId) {
        
       String hql="update AppUser set deleted=1 where appuserId=:appuserId";
       Query query=getSession().createQuery(hql);
       query.setInteger("appuserId",appAdminId.intValue());
       query.executeUpdate();
    }
	
	public List<AppAdmin> searchByCriteria(AppAdminSearchCriteria appAdminSearchCriteria, SearchCriteria searchCriteria) {

		StringBuffer hqlsb = new StringBuffer(100);
		List<Object> paramList = new ArrayList<Object>();
		//start:构建HQL语句
		hqlsb.append("select c from AppAdmin c ");

		//-start:角色
		if (StringUtils.isNotBlank(appAdminSearchCriteria.getRole())) {
			hqlsb.append(" join c.userRoles r where r.roleName = ? and  c.deleted=0 and c.appuserId!=-2 ");
			paramList.add(appAdminSearchCriteria.getRole());
		} else {
			hqlsb.append("where c.deleted=0 and c.appuserId!=-2 ");
		}
		//-end:角色

		//-start：一般域
		if (StringUtils.isNotBlank(appAdminSearchCriteria.getFieldValue())) {
			hqlsb.append(" and c.").append(appAdminSearchCriteria.getFieldName()).append(" like ?");
			paramList.add("%" + appAdminSearchCriteria.getFieldValue().trim() + "%");
		}
		//-end: 一般域
		
		//-start:姓名
		if (StringUtils.isNotBlank(appAdminSearchCriteria.getFullname())) {
			hqlsb.append(" and c.firstname like ?");
			paramList.add("%" + appAdminSearchCriteria.getFullname().trim() + "%");
		}
		//-end:姓名
		
		//-start:注册开始时间
		if (StringUtils.isNotBlank(appAdminSearchCriteria.getStartCreateTime())) {
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(), appAdminSearchCriteria.getStartCreateTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and c.createTime >= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		//-end:注册开始时间
		//-start:注册结束时间
		if (StringUtils.isNotBlank(appAdminSearchCriteria.getEndCreateTime())) {
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(), appAdminSearchCriteria.getEndCreateTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.add(Calendar.DAY_OF_YEAR, 1);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and c.createTime <= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		//-end:注册结束时间
		//-start:性别
		if (null != appAdminSearchCriteria.getGender()) {
			hqlsb.append(" and c.gender = ?");
			paramList.add(appAdminSearchCriteria.getGender());
		}
		//-end:性别
		//-start:状态
		if (null != appAdminSearchCriteria.getSearchStatus()) {
			hqlsb.append(" and c.status = ?");
			paramList.add(appAdminSearchCriteria.getSearchStatus());
		}
		//-end:状态
	
		// start:order by
		if (null != appAdminSearchCriteria.getOrderby()) {
			hqlsb.append(" order by ").append(getSortByString(appAdminSearchCriteria.getOrderby()));
		} else {
			hqlsb.append(" order by c.createTime desc");
		}
		//end:order by

		//end:构建HQL语句
		searchCriteria.setHql(hqlsb.toString());
		searchCriteria.removeParamValues();
		for (Object object : paramList) {
			searchCriteria.addParamValue(object);
		}
		List<AppAdmin> list = searchByCriteria(searchCriteria);
		return list;
	}
	
	 /**
     * 根据orderby获得 hql语句
     *
     * @param orderby 排序id
     * @return hql语句
     */
    private String getSortByString(short orderby) {
        String orderByString;
        switch (orderby) {
        	 case 1:
                 orderByString = "c.createTime asc";
                 break;
             case 2:
                 orderByString = "c.createTime desc";
                 break;
             case 3:
                 orderByString = "convert_gbk(c.firstname) asc";
                 break;
             case 4:
                 orderByString = "convert_gbk(c.firstname) desc";
                 break;
             case 5:
            	 orderByString = "convert_gbk(c.username) asc";
            	 break;
             case 6:
            	 orderByString = "convert_gbk(c.username) desc";
            	 break;
             case 7:
            	 orderByString = "c.status asc";
            	 break;
             case 8:
            	 orderByString = "c.status desc";
            	 break;
             default:
                 orderByString = "c.createTime desc";
                 break;
        }
        return orderByString;
    }
}